const Controller = require("egg").Controller;

let cla = '';

class siptime {

    test(time) {
        let rg = /Epoch Time: (\S*) seconds/;//第五行
        let rg1 = /Source Address: (\S*)/;//地三十九行
        let rg2 = /Destination Address: (\S*)/;//第四十行
        let rg3 = /Source Port: (\S*)/;//第四十二行
        let rg4 = /Destination Port: (\S*)/;//第四十三行
        let rg5 = /Session Initiation Protocol \S(\S*)\S/;//第五十二行
        let rg6 = /SIP to address Host Part: (\S*)/;//第七十八行
        let rg7 = /SIP from address Host Part: (\S*)/;//第八十三行
        let rg8 = /SIP from tag: (\S*)/;
        let rg9 = /Generated Call-ID: (\S*).]/;
        if (rg.test(time)) {
            // time = time.substring(12, 32);
            time = time.match(rg)[1];
            return time;
        }
        else if (rg1.test(time)) {
            time = time.match(rg1)[1];
            return time;
        }
        else if (rg2.test(time)) {
            time = time.match(rg2)[1];
            return time;
        }
        else if (rg3.test(time)) {
            time = time.match(rg3)[1];
            return time;
        }
        else if (rg4.test(time)) {
            time = time.match(rg4)[1];
            return time;
        }
        else if (rg5.test(time)) {
            time = time.match(rg5)[1];
            return time;
        }
        else if (rg6.test(time)) {
            time = time.match(rg6)[1];
            return time;
        }
        else if (rg7.test(time)) {
            time = time.match(rg7)[1];
            return time;
        }
        else if (rg8.test(time)) {
            time = time.match(rg8)[1];
            return time;
        }
        else if (rg9.test(time)) {
            time = time.match(rg9)[1];
            return time;
        }
        else {
            return 0;
        }

    }
}

class ClazzController extends Controller {

    //restful: index/create/destroy/update
    // 不做任何异常处理



    async index() {
        let id = this.ctx.request.query.id;
        let clazzList = await this.app.model.Clazz.findAll();
        this.ctx.body = clazzList;
    }

    async create() {
        let name = this.ctx.request.body.name;

        // 尝试以下
        let name1 = [];
        let name2 = [];
        let siptimes = new siptime();

        name.forEach((item) => {
            if (item.trim() != '') {
                item = siptimes.test(item);
                if (item) {
                    name1.push(item);
                }
            }
            else {
                name2.push(name1);
                name1 = [];
            }
        })
        //尝试一下
        let valueArr = [];
        for (let i = 0; i <= name2.length - 1; i++) {
            var value = {
                time: name2[i][0],
                srcip: name2[i][1],
                dstip: name2[i][2],
                siptype: name2[i][5],
                callid: name2[i][9],
            };
            valueArr.push(value);
        }

        cla = valueArr;
        let res = await this.app.model.Clazz.bulkCreate(valueArr);
        // 成功
        this.ctx.body = res;


    }

}



module.exports = ClazzController;